package com.deyuanyun.pic.settings.service.pipe.impl;

import com.deyuanyun.pic.common.cache.TempStoreDataSupport;
import com.deyuanyun.pic.common.dto.DTOResult;
import com.deyuanyun.pic.common.exception.ExceptionCode;
import com.deyuanyun.pic.common.exception.ExceptionCodeUtil;
import com.deyuanyun.pic.common.exception.bizcode.basedata.pipe.PipeCrossCodeEnum;
import com.deyuanyun.pic.common.exception.code.ModuleMsg;
import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.settings.controller.dto.PipeSegmentDTO;
import com.deyuanyun.pic.settings.controller.dto.PipeSegmentInfraVO;
import com.deyuanyun.pic.settings.controller.dto.PipeValidateVO;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipeLocationDTO;
import com.deyuanyun.pic.settings.domain.pipe.PipeSegment;
import com.deyuanyun.pic.settings.mapper.pipe.PipeSegmentMapper;
import com.deyuanyun.pic.settings.service.pipe.PipeLocationService;
import com.deyuanyun.pic.settings.service.pipe.PipeSegmentService;
import com.deyuanyun.pic.settings.support.utils.PipeDictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 *
 * @作者: 严祥
 * @创建时间: 2015年8月18日
 * @类名称: PipeSegmentServiceImpl.java
 * @类说明:管段操作接口
 *
 */
@Service
public class PipeSegmentServiceImpl implements PipeSegmentService {

	@Resource
	private PipeSegmentMapper pipeSegmentMapper;

    @Autowired
    private PipeLocationService pipeLocationService;


	/**
	 * 清空管段缓存
	 * @param
	 * @return
	 * @author axiao(xj08.dream@gmail.com)
	 * @date 2016-11-18
	 */
	private void clearPipeSegmentCache() {
		TempStoreDataSupport.removeData(PipeDictUtils.PIPEDICTUTILS_PIPESEGMENTBIMAP);
		TempStoreDataSupport.removeData(PipeDictUtils.PIPEDICTUTILS_PIPESEGMENTMULTIMAP);
	}

	/**
	 *
	 * @创建者:严祥
	 * @创建时间：2015年8月18日
	 * @方法说明:根据管线ID查询到该管线所属的所有管段信息
	 * @参数:@param pipeLineId:管线ID
	 * @return:DTOResult
	 *
	 */
	@Override
	public DTOResult getPipeSegmentByPipeLineId(String pipeLineId) {

		List<PipeSegment> list = null;

		list = pipeSegmentMapper.selectByPipeLineId(pipeLineId);

		if (null == list) {
			/**
			 * 返回一个空指针DTO
			 */
			return new DTOResult(
					ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA)
							+ PipeCrossCodeEnum.nullPointer.getCode(),
					"pipeSegmentMapper.selectByPipeLineId(pipeLineId)出现"
							+ PipeCrossCodeEnum.nullPointer.getLable());
		}

		if (list.size() <= 0) {
			/**
			 * 返回一个没查询到结果集的DTO
			 */
			return new DTOResult(
					ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA)
							+ PipeCrossCodeEnum.mapperIsNull.getCode(),
					PipeCrossCodeEnum.mapperIsNull.getLable());
		}

		/**
		 * 操作正常
		 */
		return new DTOResult(ExceptionCode.SUCCESS.getCode(),
				ExceptionCode.SUCCESS.getLabel(), list);

	}

	@Override
	public List<PipeSegment> getSegmentByLineId(String lineId) {
		List<PipeSegment> pipeSegmentList= TempStoreDataSupport.pullData("getSegmentByLineId_"+lineId);
		if (ObjectUtil.isEmptyAll(pipeSegmentList)){
			pipeSegmentList=pipeSegmentMapper.getSegmentByLineId(lineId);
			TempStoreDataSupport.pushDataByTimeMinute("getSegmentByLineId_"+lineId,pipeSegmentList,30);
		}
		return pipeSegmentList;
	}

	public void insert(PipeSegmentInfraVO pipeSegmentInfra) {
		String id= CommonUtil.getUUID();
		pipeSegmentInfra.setId(id);
		pipeSegmentInfra.setStatus((short)1);
		pipeSegmentInfra.setTaskStatus("03");
        String fromId = pipeSegmentInfra.getFromPipeValveroomId();
        String toId = pipeSegmentInfra.getToPipeValveroomId();
		String pipeLineId = pipeSegmentInfra.getLineNameId();
        if(StringUtils.isNotEmpty(fromId)) {
            setSegmentType(pipeSegmentInfra, pipeLineId, fromId, 1);
        }
        if(StringUtils.isNotEmpty(toId)) {
            setSegmentType(pipeSegmentInfra, pipeLineId, toId, 0);
        }
		pipeSegmentMapper.insertSegment(pipeSegmentInfra);
	}


    /**
     * 设置管段的起点、终点的类型
     * @param pipeSegmentInfraVO
	 * @param pipeLineId 管道id
     * @param valveRoomId 场站阀室id
     * @param flag 为1时设置fromType
     */
    private void setSegmentType(PipeSegmentInfraVO pipeSegmentInfraVO,String pipeLineId, String valveRoomId, int flag) {
        PipeLocationDTO locationDTO =  pipeLocationService.queryPipeLocationById(pipeLineId, valveRoomId);
        if(locationDTO != null) {
            String type = locationDTO.getType();
            if("valveroom".equalsIgnoreCase(type)) { //场站阀室
                if(flag == 1) {
                    pipeSegmentInfraVO.setFromType("VALVE_ROOM");
                } else {
                    pipeSegmentInfraVO.setToType("VALVE_ROOM");
                }
            }
            if("sign".equalsIgnoreCase(type)) { //桩
                if(flag == 1) {
                    pipeSegmentInfraVO.setFromType("IDENTIFICATION");
                } else {
                    pipeSegmentInfraVO.setFromType("IDENTIFICATION");
                }
            }
        }
    }

	@Override
	public List<PipeSegmentDTO> findByCondition(PipeSegmentInfraVO pipeSegmentInfra) {
		return pipeSegmentMapper.findByCondition(pipeSegmentInfra);
	}

	public boolean validateRepBySegment(PipeValidateVO pipeValidateVO) {
		int n = pipeSegmentMapper.validateRepBySegment(pipeValidateVO);
        if(n == 0)
            return false;
        return true;
	}

	public int deleteSegmentById(String[] ids) {
		int n = pipeSegmentMapper.removeSegmentById(ids);
		if(n > 0) {
			clearPipeSegmentCache();
		}
		return n;
	}

	public int updateStatus(String[] ids) {
		int n = pipeSegmentMapper.updateStatus(ids);
		if(n > 0) {
			clearPipeSegmentCache();
		}
		return n;
	}

	public int updateSegmentS(String[] ids, String status) {
		int n = pipeSegmentMapper.updateSegmentS(ids, status);
		if(n > 0) {
			clearPipeSegmentCache();
		}
		return n;
	}

	/**
	 * @Description: 管道初始化：根据id修改数据
	 * @author h.x.rong
	 * @date 2016年1月5日
	 * @param
	 * @return void
	 */
	public void updatePipeSegment(PipeSegmentInfraVO pipeSegmentInfraVO) {
        String fromId = pipeSegmentInfraVO.getFromPipeValveroomId();
        String toId = pipeSegmentInfraVO.getToPipeValveroomId();
        String pipeLineId = pipeSegmentInfraVO.getLineNameId();
        if(StringUtils.isNotEmpty(fromId)) {
            setSegmentType(pipeSegmentInfraVO, pipeLineId, fromId, 1);
        }
        if(StringUtils.isNotEmpty(toId)) {
            setSegmentType(pipeSegmentInfraVO, pipeLineId, toId, 0);
        }
		pipeSegmentMapper.updatePieSegment(pipeSegmentInfraVO);
		clearPipeSegmentCache();
	}

	@Override
	public List<PipeSegment> getAllSegment(Map map){
		return pipeSegmentMapper.queryAllSegment(map);
	}


	@Override
	public List<Map> querySegmentListByLineId(String LineId) {
		return pipeSegmentMapper.querySegmentListByLineId(LineId);
	}

	
    @Override
    public List<PipeSegment> queryPipeSegment(String pipeLineId,String pipeSegmentName) {
        return pipeSegmentMapper.queryPipeSegment(pipeLineId, pipeSegmentName);
    }

	
	
}
